package MySQL::Query::Extract::ORDERBY;

use strict;
use warnings;

use base qw(MySQL::Query::Extract);
use Carp;

my $token_class = 'DBIx::MyParsePP::Token';
my $rule_class = 'DBIx::MyParsePP::Rule';

sub parse_tree     { return shift->tree->tree }
sub tokens         { return shift->tree->tokens(@_) }

sub process {
      my ($self , $sel , $extract) = @_;
	
      my $orderby_tree = $self->parse_tree->getOrderBy(); #getting order by clause
      if ($orderby_tree) {
	    $orderby_tree = $orderby_tree->shrink();
	    
            my $data = ($orderby_tree->children())[2]; #we need only this
	    if ($data->[0] ne 'order_list') {
		 $data = [$data];
	    }
            
            for (my $i = 0; $i <= $#{$data}; $i++) { #search for our table
		  if (ref $data->[$i] eq $token_class) {
			# it could be ASC|DESC or it could be a column
			# which is an alias for some column in SELECT statement
			# like SELECT id AS my ORDER BY my
				
			# here we don't care if it's an ASC|DESC, we only care for alias
			next if ($data->[$i]->value =~ /asc|ASC|DESC|desc|,/);
				
			# so now, if we have found anything, it's should be an alias for some
			# column in a SELECT statement
			# and because we ran select statement first of all, we should
			# have that alias presented in a $sel, so let's search for it
			
			my $iterator = $sel->cols->children; 
			if ($iterator) {
			      while($iterator->pull)  {
				    my $current_object = $iterator->current;
				    if (
					  $current_object->alias &&
					  $current_object->alias eq $data->[$i]->value
				    ) {
					  # add it to that $sel
					  my $order = SQL::Builder::Order->new();	
					  $order->expr($data->[$i]->value());
					  $order->asc;
					  
					  if (ref $data->[$i + 1] eq $token_class) {
						# we have ASC or DESC, we should modify last added clause
						# it could be a bug here, i should check
						$order->desc() if ($data->[$i + 1]->value() =~ /desc|DESC/);
					  }
					  
					  $sel->orderby->list_push($order);
				    }
			      }
			}
		  } elsif (ref $data->[$i] eq $rule_class) {
			
                        # so now it's more simple
			if (
			    $self->cmp_names($data->[$i]->[1]->value() , $extract)
			) {
			      my $order = SQL::Builder::Order->new();
			      
			      my $order_temp = $data->[$i]->[1]->value() . '.' . $data->[$i]->[3]->value();	
			      $order->expr($order_temp);
			      $order->asc;
					
			      if (ref $data->[$i + 1] eq $token_class) {
				    # we have ASC or DESC, we should modify last added clause
				    # it could be a bug here, i should check
				    if ($data->[$i + 1]->value() =~ /desc|DESC/) {
					  $order->desc;
				    } elsif ($data->[$i + 1]->value() =~ /asc|ASC/) {
					  $order->asc;
				    }
			      }
			      
			      $sel->orderby->list_push($order);
			}
		  } 
	    }
      }
}


1;

=head1 NAME

MySQL::Query::Extract::ORDERBY

=head1 SYNOPSIS
      
=head1 DESCRIPTION

=head1 METHODS

=head2 new()

=head2 init()

=head2 process()
